'use client';

import { useToast } from '@/hooks/use-toast';
import { result } from '@/lib/utils';
import { adminUpdateShopStatus } from '@/service/actions/shop';
import type { ShopSummary } from '@/service/type/shop';
import ConfirmButton from '../common/confirm-button';
import { Button } from '../ui/button';
import ReviewButton from './review-button';
import WarningButton from './warning-button';

export default function ShopActionButton({
  shopData,
  variant = 'default',
}: { shopData: ShopSummary; variant?: 'ghost' | 'outline' | 'default' }) {
  const { toast } = useToast();

  if (shopData.status === 'reviewing')
    return (
      <ReviewButton
        shopId={shopData.id}
        button={<Button variant={variant}>审核</Button>}
        licenseKey={shopData.licenseKey}
        hygieneCertKey={shopData.hygieneCertKey}
      />
    );

  if (shopData.status === 'normal')
    return (
      <WarningButton
        shopId={shopData.id}
        button={<Button variant={variant}>警告</Button>}
      />
    );

  if (shopData.status === 'warning')
    return (
      <ConfirmButton
        title="解除警告"
        description="确认解除该店铺的警告吗？"
        button={<Button variant={variant}>解除警告</Button>}
        onClick={async () => {
          const { error } = await adminUpdateShopStatus({
            shopId: shopData.id,
            status: 'normal',
          });
          error
            ? toast({ title: error.message, variant: 'destructive' })
            : toast({ title: '解除成功' });
          return error ? result(error, error.message) : result();
        }}
      />
    );

  return null;
}
